import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/Login.vue'
import Home from '../components/Home.vue'
import Register from '../components/Register.vue'
import MangerIndex from '../components/Manger.vue'
const HomePage = () => import('../components/views/homepage/HomePage.vue')
const ProfessionQuery = () => import('../components/views/professionquery/ProfessionQuery.vue')
const Recommend = () => import('../components/views/recommend/Recommend')
const SchoolQuery = () => import('../components/views/schoolquery/SchoolQuery')
const InsertSchool = () => import('../components/views/mangerviews/InsertSchool')
const UpdateSchool = () => import('../components/views/mangerviews/UpdateSchool')
const DeleteSchool = () => import('../components/views/mangerviews/DeleteSchool')
const InsertProfession = () => import('../components/views/mangerviews/InsertProfession')
const UpdateProfession = () => import('../components/views/mangerviews/UpdateProfession')
const DeleteProfession = () => import('../components/views/mangerviews/DeleteProfession')
//解决重复点报错问题
const originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/register',
    component: Register
  },
  {
    path: '/manger',
    component: MangerIndex,
    // redirect:'/manger/insertschool',
    children: [
      {
        path: 'insertschool',
        component: InsertSchool
      },
      {
        path: 'updateschool',
        component: UpdateSchool
      },
      {
        path: 'deleteschool',
        component: DeleteSchool
      },
      {
        path: 'insertprofession',
        component:InsertProfession
      },
      {
        path: 'updateprofession',
        component:UpdateProfession
      },
      {
        path: 'deleteprofession',
        component:DeleteProfession
      }
    ]
  },
  {
    path: '/home',
    component: Home,
    children: [
      {
        path: '',
        redirect:'homepage'
      },
      {
        path: 'homepage',
        component:HomePage
      },
      {
        path: 'professionquery',
        component:ProfessionQuery
      },
      {
        path: 'recommend',
        component:Recommend
      },
      {
        path: 'schoolquery',
        component:SchoolQuery
      }
      
    ]
  },
]


const router = new VueRouter({
  mode: 'history',
  routes,
  linkActiveClass: 'active'
})

//  挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 表示将要访问的路径
  // from 表示从哪个路径跳转而来
  // next是一个函数，表示放行
  // next()  放行     next('/login')  强制跳转
  if (to.path === '/login') return next();
  if (to.path === '/register') return next();
  // if (to.path === '/manger') return next();
  //获取token
  const tokenStr = window.sessionStorage.getItem('token');
  if (!tokenStr) return next('/login');
  next();
})


/* 路由异常错误处理，尝试解析一个异步组件时发生错误，重新渲染目标页面 */
router.onError((error) => {
  const pattern = /Loading chunk (\d)+ failed/g;
  const isChunkLoadFailed = error.message.match(pattern);
  const targetPath = router.history.pending.fullPath;
  if (isChunkLoadFailed) {
    router.replace(targetPath);
  }
});
export default router
